package com.google.code.bankagregator.adapter.bank.imex.parser.credit;

import com.google.code.bankagregator.adapter.exception.AdapterConnectException;
import com.google.code.bankagregator.entities.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kirill
 * Date: 14.06.13
 * Time: 1:10
 * To change this template use File | Settings | File Templates.
 */
public class ImexConsCreditParser {
    private Bank bank;
    private static final String CREDIT_URL = "http://imexbank.com.ua/rus/credit_nozal.phtml";
    private static final int INDEX_UL_WITH_PARAMS = 0;
    private static final int INDEX_WITH_TERM = 1;
    private static final int INDEX_WITH_MIN_AMOUNT = 2;
    private static final int INDEX_WITH_PREPAYMENT = 4;
    private static final int INDEX_MIN_AMOUNT = 4;
    private static final int INDEX_MAX_AMOUNT = 6;
    private static final String SELECT_PARAM = "div[class=add-info-content] ul";
    Document con;
    Credit credit;

    public ImexConsCreditParser() throws AdapterConnectException {
        bank = new Bank();
        bank.setName("АО \"ИМЭКСБАНК\"");
        bank.setUrl("http://imexbank.com.ua/rus/main.phtml");
        try {
            con = Jsoup.connect(CREDIT_URL).timeout(20000).get();
        } catch (IOException e) {
            throw new AdapterConnectException(e.getMessage());
        }
        credit = new Credit();
    }

    /**
     * Get consumer`s credit`s params
     *
     * @return credit`s params
     */
    public List<Credit> getCredit() {

        List<Credit> creditParam = new ArrayList<>();
        Element rateEl = con.select(SELECT_PARAM).get(INDEX_UL_WITH_PARAMS).select("li").first();
        Element termEl = con.select(SELECT_PARAM).get(INDEX_UL_WITH_PARAMS).select("li").get(INDEX_WITH_TERM);
        Element minAmountEl = con.select(SELECT_PARAM).get(INDEX_UL_WITH_PARAMS).select("li").get(INDEX_WITH_MIN_AMOUNT);
        String[] amountParams = minAmountEl.text().split(" ");
        Element prepaymentEl = con.select(SELECT_PARAM).get(INDEX_UL_WITH_PARAMS).select("li").get(INDEX_WITH_PREPAYMENT);

        double prepayment = Double.parseDouble(prepaymentEl.text().substring(8, 9));
        int minAmount = Integer.parseInt(amountParams[INDEX_MIN_AMOUNT]);
        int maxAmount = Integer.parseInt(amountParams[INDEX_MAX_AMOUNT]);
        int term = Integer.parseInt(termEl.text().replaceAll("[^0-9.]+", "")) * 31;
        double rate = Double.parseDouble(rateEl.text().replaceAll("[^0-9.]+", "").replace(",", "."));

        credit.setPrepayment(prepayment);
        credit.setMinAmount(minAmount);
        credit.setMaxAmount(maxAmount);
        credit.setTerm(term);
        credit.setCreditRate(rate);
        credit.setState(State.ACTIVE);
        credit.setCurrency(Currency.UAH);
        credit.setType(CreditType.CONSUMER);
        credit.setName("Потребительский кредит");
        credit.setUrl(CREDIT_URL);
        credit.setBank(bank);
        creditParam.add(credit);
        return creditParam;
    }
}
